<?php

namespace Controllers;

class ControllerAbstract {
       
    protected $_di;
    
    protected $_view;
    
    protected $_request;
    
    public function __construct($request, $viewModelBuilder, $di) {
        $this->_request = $request;
        $this->_view = $viewModelBuilder;
        $this->_di = $di;
    }
    
    public function post($params, $filter = FILTER_DEFAULT) {
        
        if (is_array($params)) {
            return filter_input_array(INPUT_POST, $params);
        } else {
            return filter_input(INPUT_POST, $params, $filter);
        }
    }
    
    public function get($params, $filter = FILTER_DEFAULT) {
        
        if (is_array($params)) {
            return filter_input_array(INPUT_GET, $params);
        } else {
            return filter_input(INPUT_GET, $params, $filter);
        }
    }
    
    public function renderView($params = []) {
    
        $sessionStorage = $this->_di->getSessionStorage();
        
        $params['session'] = [
            'member' => $sessionStorage->get('member')
        ];
        
        $this->_view->build($params);
    }
    
    public function redirect($location) {
        header('location: ' . $location);
        exit;
    }
    
    public function sessionStorage() {
        return $this->_di->getSessionStorage();
    }
    
    public function isMemberLoggedIn() {
        $member = $this->sessionStorage()->get('member');
        return !empty($member);
    }
            
}
